/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.comfacauca.xmlc2.utils;

import com.comfacauca.xmlc2.servlet.OcupacionServlet;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author eorozco
 */
public class Utiles {
    
    public static Date stringToDate(String fechaStr) throws ParseException{        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
        Date fechaDt = null;        
        sdf.setLenient(false); 
        return sdf.parse(fechaStr);
    }
    
    public static Date stringToDate1(String d1){
        
        
        String[] splitdata = d1.split("-");

        int year = Integer.parseInt(splitdata[0]);
        int month = Integer.parseInt(splitdata[1])-1;
        int day = Integer.parseInt(splitdata[2]);

        Calendar cal = Calendar.getInstance(Locale.CANADA);
        cal.set(year, month, day);
        Date d = cal.getTime();
        
        return d;
    }
    
    public static String md5(String input) {
         
        String md5 = null;         
        if(null == input) return null;         
        try {             
        //Create MessageDigest object for MD5
        MessageDigest digest = MessageDigest.getInstance("MD5");         
        //Update input string in message digest
        digest.update(input.getBytes(), 0, input.length()); 
        //Converts message digest value in base 16 (hex) 
        md5 = new BigInteger(1, digest.digest()).toString(16); 
        } catch (NoSuchAlgorithmException e) { 
            e.printStackTrace();
        }
        return md5;
    }
    
    
      public static int calcularDiasRangoFechas(String f1, String f2){
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
       
            Date startDate;
            Date startDate1;
            int i=0;  
            try {
                startDate = sdf.parse(f1);
                Date endDate = sdf.parse(f2);

                Calendar start = Calendar.getInstance();
                start.setTime(startDate);
                Calendar end = Calendar.getInstance();
                end.setTime(endDate);

                            
                for (Date date = start.getTime(); !start.after(end); start.add(Calendar.DATE, 1), date = start.getTime()) {                    
                    i++;
                }                
       

            } catch (ParseException ex) {
                Logger.getLogger(OcupacionServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            return i;
  }
    
      
public static void enviarCorreo2(String to, String asunto, String mensaje)
  {
    Properties properties = System.getProperties();

    String from = "analistasistemas2@comfacauca.com";
    String clave= "Sarita6107";
    String server= "zimbra.comfacauca.com"; 
    
    String host = server;

    properties.setProperty("mail.smtp.host", host);
    properties.setProperty("mail.smtp.port", "25");

    properties.setProperty("mail.smtp.user", "eorozco");
    properties.setProperty("mail.smtp.password", "e102030");
    properties.setProperty("mail.smtp.auth", "true");
    properties.setProperty("mail.debug", "true");

    Session session = Session.getDefaultInstance(properties);
    try
    {
      MimeMessage message = new MimeMessage(session);

      message.setFrom(new InternetAddress(from));

      message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

      message.setSubject(asunto);

      message.setText(mensaje);

      Transport transport = session.getTransport("smtp");
      transport.connect(null, from, clave);
      message.saveChanges();
      transport.sendMessage(message, message.getAllRecipients());

      System.out.println("Sent message successfully....");
    } catch (MessagingException mex) {
      mex.printStackTrace();
    }
  }

      
    
}
